﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Diagnostics;
namespace oled
{
    public partial class Simulator : Form
    {
        Graphics oled_Graphic;
        Byte[,] oled_Screen;
        public Simulator()
        {
            InitializeComponent();
            PortListGet(); 
        }

        static void new_Thread(string[] args)
        {
            string hello = "hello world";

            Thread thread = new Thread(() => ThreadMainWithParameters(hello));
            thread.Start();
        }

        static void ThreadMainWithParameters(string str)
        {
        }

        private void OLED_Demo2()
        {
            //刷图
            int x = 0, y = 0; 
            Stopwatch sw = new Stopwatch();
            sw.Start();

            Oled_Drive.DrawBMP1 (0, 0, Fonts.BMP_Demo,44, 44, oled_Graphic);

            sw.Stop();
            Oled_Drive.ShowString(65, 8, "time(.0ms):" + sw.ElapsedMilliseconds, oled_Graphic);
            Oled_Drive.ShowString(65, 16,"tick(.1us):" + sw.ElapsedTicks, oled_Graphic);
            int rate = (int) (10 - sw.ElapsedMilliseconds);
            if (rate > 0)
            {
                Thread.Sleep(rate);
            }
        }

        private void OLED_Demo1()
        {
            //直接使用Draw_String/Char函数
            String[] menu = { "List1", "List2", "List3", "List4" };
            int y = 0, i = 0;
            for (y = 0; y < menu.Length * 8; y += 9)
            {
                Oled_Drive.ShowString(2, y + 2, menu[i], oled_Graphic);
                i++;
            }
        }
        private void KeyBoard_Event(int keyvalue)
        {
             switch (keyvalue)
            {
                case 65:
                    OLED_Demo1();
                    break;
                case 66:
                    Thread Demo = new Thread(OLED_Demo2);
                    Demo.Start();
                    break;
                     
                case 27:
                    ESC_Click_Event();
                    break;
                case 37: //Left

                    break;
                case 38: //UP

                    break;
                case 39: //Right

                    break;
                case 40: //Down

                    break;
                default:
                    break;
            }
        }

        private bool User_Controls_Visible_Get()
        {
           return this.Enter.Visible;
        }

        private void User_Controls_Visible_Set(bool visiable)
        {

            this.OLED_Height.Visible = visiable;
            this.OLED_Width.Visible = visiable;
            this.OLED_FPS.Visible = visiable;
            this.label_Use_Serial.Visible = visiable;
            this.label_Height.Visible = visiable;
            this.label_Width.Visible = visiable;
            this.label_FPS.Visible = visiable;
            this.PortList.Visible = visiable;
            this.Enter.Visible = visiable;
        }

        // ESC键
        private void ESC_Click_Event()
        {
            int X, Y;

                this.oled.Visible = false;
                this.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;

                WinForm_Contrl.Center_Get(this, out X, out Y);
                WinForm_Contrl.Size_Set(this.oled, 160, 160);
                WinForm_Contrl.Size_Set(this, 200, 170);
                WinForm_Contrl.Center_Set(this, X, Y);

                User_Controls_Visible_Set(true);
        }

        // 确认键
        private void Enter_Click(object sender, EventArgs e)
        {
            int OLED_Height, OLED_Width,X, Y;
            bool height = int.TryParse(this.OLED_Height.Text, out OLED_Height);
            bool width = int.TryParse(this.OLED_Width.Text, out OLED_Width);
            bool visiable = this.Enter.Visible;


            if (height && width && OLED_Height >= 32 && OLED_Width >= 32)
            {
                oled_Screen = Buff.Create(OLED_Height, OLED_Width);

                Oled_Drive.Set_DPI(OLED_Height, OLED_Width);
                Oled_Drive.Set_PaintPanel(ref OLED_Height, ref OLED_Width);

                WinForm_Contrl.Center_Get(this, out X, out Y);
                WinForm_Contrl.Size_Set(this.oled, OLED_Height, OLED_Width);
                WinForm_Contrl.Size_Set(this, OLED_Height + 52, OLED_Width + 30);
                WinForm_Contrl.Center_Set(this, X, Y);
                this.BackColor = System.Drawing.SystemColors.HotTrack;
                this.oled_Graphic = this.oled.CreateGraphics();
                this.oled.Visible = true;

                User_Controls_Visible_Set(false);
            }
        }

        // 键盘按键
        private void oled_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.oled.Visible == true)
            {
                KeyBoard_Event(e.KeyValue);
            }
        }

        // 点击屏幕事件
        private void oled_Click(object sender, EventArgs e)
        {
            Oled_Drive.Clear(this.oled);
        }

        private void Simulator_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
            this.KeyDown += new KeyEventHandler(oled_KeyDown);
        }

        // 获取串口号
        private void PortListGet()
        {
            PortList.Items.Clear();
            string[] PortGet = SerialPort.GetPortNames();                  //port list add
            PortList.Items.Add("None");
            if (PortGet.Length != 0 && PortGet != null)                    //数组为空，且长度为0  (判断数组为空的方法)
            {
                for (Byte i = 0; i < PortGet.Length; i++)
                {
                    PortList.Items.Add(PortGet[i]);
                }
                PortList.SelectedIndex = 0;
            }
        }

        private void SerialPort_Click(object sender, EventArgs e)
        {
            PortListGet();
        }

    }


    public class Buff
    {
        public static Byte[,] Create(int height, int width)
        {
            Byte[,] buff = new Byte[height, width];
            return buff;
        }
    }
}
